from DrissionPage import Chromium
from DrissionPage import ChromiumOptions

import concurrent.futures
from DataRecorder import Recorder
r = Recorder(path='data.csv', cache_size=500)




# 创建浏览器启动配置对象
options = ChromiumOptions()
# 配置浏览器启动选项

options.ignore_certificate_errors() # 忽略证书错误
# options.

# options.set_local_port(9709)  # 设置浏览器debug端口

browser = Chromium(options)

# 设置要访问的网址
url='https://f.qianzhan.com/yuanqu/diqu/3503/'

# 获取标签页对象并打开指定的网址
tab = browser.new_tab(url)
tab.wait(5)


# -----

数据列表=[]
while True:
    tab.wait(1)
    for tr  in tab('t:tbody').eles('t:tr'):
        链接=tr.ele('详情').link        
        pp=tr.texts()+[链接]
        print(pp)
        数据列表.append(pp)

    bb=tab.ele('t:a@@class=cur',timeout=3)
    if bb.text!='10':
        print(f'有..... {bb}')
        tab('下一页').click()
        tab.wait(1)
    else:
        print('没有下一页了...............##########')
        break    

        

def get_zuobiao(data):
    url=data[-1]
    
    t=browser.new_tab()
    t.wait(1,2)
    t.get(url)
    iframe=t.ele('t:iframe')
    t.wait(1)
    zuobiao=iframe.run_js('return window._center')
    data.append(zuobiao)
    r.add_data(data)


    t.close()
    return data






# 创建一个线程池，最大线程数为5
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # 提交多个任务到线程池
    tasks = [executor.submit(get_zuobiao, i) for i in 数据列表]

    # 获取任务的结果
    for future in concurrent.futures.as_completed(tasks):
        result = future.result()
        print(f"任务结束..: {result}")

#
r.record()
input('Press any key to quit....................')

# print(aa)